package dao

import (
	"soul/initiate"
	"soul/model"
)

func FindReleaseByLike() (article []model.Release) {
	err := initiate.DB.Order("like_num desc").Find(&article).Error
	if err != nil {
		return []model.Release{}
	}
	return article
}

func CreateRelease(title string, types int, content string, user uint) (article model.Release) {
	article = model.Release{
		UserId:  int(user),
		Title:   title,
		Content: content,
		Types:   types,
	}
	err := initiate.DB.Create(&article).Error
	if err != nil {
		return model.Release{}
	}
	return article
}

func FindReleaseById(user uint) (article model.Release) {
	err := initiate.DB.Where("user_id =?", user).Find(&user).Error
	if err != nil {
		return model.Release{}
	}
	return article
}

func FindFollByType() (follow []model.Follow) {
	err := initiate.DB.Order("types asc").Find(&follow).Error
	if err != nil {
		return []model.Follow{}
	}
	return follow
}

func CreateReleaseLike(titleId int) (like model.Like) {
	like = model.Like{
		TitleId: titleId,
	}
	err := initiate.DB.Create(&like).Error
	if err != nil {
		return model.Like{}
	}
	return like
}

func DeleteReleaseLike(titleId int) (like model.Like) {
	like = model.Like{
		TitleId: titleId,
	}
	err := initiate.DB.Where("title_id=?", titleId).Delete(&like).Error
	if err != nil {
		return model.Like{}
	}
	return like
}

func CreateCommentLike(titleId int, content string, likes int) (comment model.Comment) {
	comment = model.Comment{
		TitleId: titleId,
		Content: content,
		Likes:   likes,
	}
	err := initiate.DB.Create(&comment).Error
	if err != nil {
		return model.Comment{}
	}
	return comment
}

func CreateReplyComment(commentId int, replys string) (repy model.Reply) {
	repy = model.Reply{
		CommentId: commentId,
		Replys:    replys,
	}
	err := initiate.DB.Create(&repy)
	if err != nil {
		return model.Reply{}
	}
	return repy
}

func FindListLike() (comment []model.Comment) {
	err := initiate.DB.Find(&comment).Error
	if err != nil {
		return []model.Comment{}
	}
	return comment
}

func FindReleasesByLikeNumAndCommentNum() (releases []model.Release) {
	err := initiate.DB.Find(&releases).Error
	if err != nil {
		return []model.Release{}
	}
	return releases
}
